<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 页面的部分的数据准备
 * 
 * @desc    获取页面中各个 section 的数据
 * @version 1.0.0 
 * @author  Jason
 * @date    2017年5月2日 下午1:00:44
 */
class Lib_section
{
	/** @var CI_Controller */
	private $CI;
	
	public function __construct()
	{
		$this->CI =& get_instance();
	}
	
	/**
	 * 客户端通用的 COOKIE 句柄（ common_cookie_key ）
	 */
	public function common_cookie()
	{
		// 跨站请求伪造（CSRF）(Cross-Site Request Forgery，通常缩写为CSRF或者XSRF)
		$csrf = [
				'name' => $this->CI->security->get_csrf_token_name(),
				'hash' => $this->CI->security->get_csrf_hash()
		];
		$cookie['csrf'] = $csrf;
		
		$keys = [
				'token_key',
				'language_key',
				'currency_key',
				'timezone_key',
				'remember_key',
				'search_history_key',
				
		];
		$this->CI->config->load('cookie');
		foreach ($keys as $key) {
			$cookie[$key] = $this->CI->config->item($key);;
		}
		
		return $cookie;
	}
	
	public function common_domain()
	{
		$domain = [
				'static' => $this->CI->config->item('static_url'),
				'web'    => $this->CI->config->item('base_url'),
				'api'    => $this->CI->config->item('base_url'),
				'attach' => $this->CI->config->item('static_url'),
		];
		
		return $domain;
	}
	
	public function common_head()
	{
		$title      	= 'Segway Discovery';
		$keywords   	= 'Segway Discovery';
		$description	= 'Segway Discovery';
		
		$result = [
				'title'      	=> $title,
				'keywords'   	=> $keywords,
				'description'	=> $description,
		];
		
		return $result;
	}
	
	public function common_header()
	{
		$this->CI->load->config('language', !true);
		$language_mapper = $this->CI->config->item('supported_languages', 'multi_language');
		$currency_mapper = $this->CI->config->item('supported_currencies', 'multi_currency');
		$timezone_mapper = $this->CI->config->item('supported_timezones', 'multi_timezone');
		
		$current_language = $this->CI->lib_cookie->get_language();
		$current_currency = $this->CI->lib_cookie->get_currency();
		$current_timezone = $this->CI->lib_cookie->get_timezone();
		
		$this->CI->load->library('lib_passport');
		$logined_member = $this->CI->get_logined_member();
		
		$result = [
				'language_mapper' => $language_mapper,
				'currency_mapper' => $currency_mapper,
				'timezone_mapper' => $timezone_mapper,
				'current_language' => $current_language,
				'current_currency' => $current_currency,
				'current_timezone' => $current_timezone,
				'logined_member' => $logined_member,
		];
		
		return $result;
	}
	
	public function index_carousel()
	{
		$this->CI->load->library('lib_advertisement');
		$result = $this->CI->lib_advertisement->get_ad_list_by_position_code('index_carousel');
		
		return $result;
	}
	
	public function index_search()
	{
		$this->CI->load->config('language', !true);
		$abbr_mapper = $this->CI->config->item('languages_abbr', 'multi_language');
		$current = $this->CI->lib_cookie->get_language();
		$language = @$abbr_mapper[$current];
		
		$this->CI->config->load('google');
		
		$api_key = $this->CI->config->item('key', 'api');
		
		$config = [
				'google_ak' => $api_key,
				'language' => $language,
		];
		
		return $config;
	}
	
	public function common_search_get_poi_list($country, $city, $language)
	{
		$this->load =& $this->CI->load;
		$this->lib_merchant =& $this->CI->lib_merchant;
		$this->lib_touroute =& $this->CI->lib_touroute;
		
		$this->load->library('lib_merchant');
		$list = $this->lib_merchant->get_merchants_as_poi($country, $city, $language);
		$list = array_map(function($item) {
			$item['type'] = 'merchant';
			return $item;
		}, $list);
		$this->load->helper('array');
		// $list = arraysFieldAss($list, ['lat'=>'latitude', 'official'=>'is_official']);
		$list = arraysColumn($list, ['type' , 'id', 'cover', 'name', 'latitude', 'longitude']);
		$list_merchant = $list;
		//print_R($list);
		
		$this->load->library('lib_touroute');
		$list = $this->CI->lib_touroute->get_touroutes_as_poi($country, $city, $language);
		$list = array_map(function($item) {
			$item['type'] = 'touroute';
			return $item;
		}, $list);
		$list = arraysColumn($list, ['type' , 'id', 'cover', 'name', 'latitude', 'longitude']);
		$list_tourouter = $list;
		//print_R($list);
		
		$content = array_merge($list_merchant, $list_tourouter);
		
		return $content;
	}
	
	// 需求更改，2只有商户
	public function common_search_get_poi_list2($country, $city, $language)
	{
		$this->load =& $this->CI->load;
		$this->lib_merchant =& $this->CI->lib_merchant;
		
		$this->load->library('lib_merchant');
		$list = $this->lib_merchant->get_merchants_as_poi($country, $city, $language);
		$this->load->helper('array');
		// $list = arraysFieldAss($list, ['lat'=>'latitude', 'official'=>'is_official']);
		$list = arraysColumn($list, ['id', 'cover', 'name', 'latitude', 'longitude']);
		//print_R($list);
		
		return $list;
	}
	
	public function common_search_get_poi_autocomplete($keyword, $country, $city, $language)
	{
		$this->load =& $this->CI->load;
		$this->lib_merchant =& $this->CI->lib_merchant;
		$this->lib_touroute =& $this->CI->lib_touroute;
		$this->lib_language =& $this->CI->lib_language;
		
		
		$this->load->library('lib_merchant');
		$idsm = $this->lib_merchant->get_ids_for_autocomplete($country, $city);
		$idsm[] = -1;
		
		$this->load->library('lib_touroute');
		$idst = $this->lib_touroute->get_ids_for_autocomplete($country, $city);
		$idst[] = -1;
		
		$module_resources_pairs = [
				'merchant' => $idsm,
				'touroute' => $idst,
		];
		$search_by = ['name'];
		$list = $this->lib_language->get_list_for_search_in_multi_module($keyword, $module_resources_pairs, $language, $search_by);
		
		$this->CI->load->helper('array');
		$rule = ['type', 'id', 'name'];
		$list = arraysFieldAss($list, ['type'=>'module', 'name'=>'field_value', 'id'=>'resource']);
		$content = arraysColumn($list, $rule);
		
		return $content;
	}
	
	public function index_recommend($language)
	{
		$this->load =& $this->CI->load;
		$this->lib_touroute =& $this->CI->lib_touroute;
		$this->lib_language =& $this->CI->lib_language;
		
		$conditions = [
				
		];
		$this->load->library('lib_touroute');
		$list = $this->lib_touroute->get_touroutes_for_list($conditions, $language, $page = 1, $number = 4, $status_tags = ['enabled']);
		
		return $list;
	}
}